@echo off
setlocal enabledelayedexpansion
chcp 65001 > nul

:: 项目配置
set PROJECT_NAME=weibo-crawler
set IMAGE_NAME=weibo-crawler
set VERSION=latest

:: 颜色定义（Windows 10及以上版本支持）
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set RED=%ESC%[31m
set GREEN=%ESC%[32m
set YELLOW=%ESC%[33m
set BLUE=%ESC%[34m
set NC=%ESC%[0m

:: 打印带颜色的消息
goto :main

:print_info
echo %BLUE%[INFO]%NC% %~1
goto :eof

:print_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

:: 检查Docker是否安装
:check_docker
docker --version > nul 2>&1
if %errorlevel% neq 0 (
    call :print_error "Docker未安装，请先安装Docker Desktop"
    exit /b 1
)

docker info > nul 2>&1
if %errorlevel% neq 0 (
    call :print_error "Docker未运行，请启动Docker Desktop"
    exit /b 1
)
goto :eof

:: 构建镜像
:build_image
call :print_info "开始构建Docker镜像..."

if not exist "Dockerfile" (
    call :print_error "Dockerfile不存在"
    exit /b 1
)

if not exist "requirements.txt" (
    call :print_error "requirements.txt不存在"
    exit /b 1
)

docker build -t %IMAGE_NAME%:%VERSION% . --no-cache
if %errorlevel% equ 0 (
    call :print_success "Docker镜像构建成功"
) else (
    call :print_error "Docker镜像构建失败"
    exit /b 1
)
goto :eof

:: 运行容器
:run_container
call :print_info "启动容器..."

if not exist "config.json" (
    call :print_warning "config.json不存在，请确保配置文件正确"
)

:: 创建必要的目录
if not exist "weibo" mkdir weibo
if not exist "log" mkdir log

:: 停止并删除已存在的容器
docker stop %PROJECT_NAME% > nul 2>&1
docker rm %PROJECT_NAME% > nul 2>&1

:: 运行容器
docker run -d ^
    --name %PROJECT_NAME% ^
    --restart unless-stopped ^
    -v "%cd%\config.json:/app/config.json:ro" ^
    -v "%cd%\weibo:/app/weibo" ^
    -v "%cd%\log:/app/log" ^
    -e TZ=Asia/Shanghai ^
    %IMAGE_NAME%:%VERSION%

if %errorlevel% equ 0 (
    call :print_success "容器启动成功"
    call :print_info "容器名称: %PROJECT_NAME%"
    call :print_info "查看日志: docker logs %PROJECT_NAME%"
    call :print_info "停止容器: docker stop %PROJECT_NAME%"
) else (
    call :print_error "容器启动失败"
    exit /b 1
)
goto :eof

:: 使用docker-compose运行
:run_compose
call :print_info "使用docker-compose启动服务..."

if not exist "docker-compose.yml" (
    call :print_error "docker-compose.yml不存在"
    exit /b 1
)

:: 创建必要的目录
if not exist "weibo" mkdir weibo
if not exist "log" mkdir log

:: 启动服务
docker-compose up -d
if %errorlevel% equ 0 (
    call :print_success "服务启动成功"
    call :print_info "查看服务状态: docker-compose ps"
    call :print_info "查看日志: docker-compose logs -f"
    call :print_info "停止服务: docker-compose down"
) else (
    call :print_error "服务启动失败"
    exit /b 1
)
goto :eof

:: 停止并删除容器
:stop_container
call :print_info "停止并删除容器..."

docker ps -a --format "table {{.Names}}" | findstr /r "^%PROJECT_NAME%$" > nul
if %errorlevel% equ 0 (
    docker stop %PROJECT_NAME%
    docker rm %PROJECT_NAME%
    call :print_success "容器已停止并删除"
) else (
    call :print_warning "容器不存在或未运行"
)
goto :eof

:: 清理镜像
:clean_image
call :print_info "清理Docker镜像..."

:: 删除项目镜像
docker images --format "table {{.Repository}}:{{.Tag}}" | findstr /r "^%IMAGE_NAME%:%VERSION%$" > nul
if %errorlevel% equ 0 (
    docker rmi %IMAGE_NAME%:%VERSION%
    call :print_success "镜像已删除"
) else (
    call :print_warning "镜像不存在"
)

:: 清理悬空镜像
docker image prune -f > nul
call :print_success "悬空镜像已清理"
goto :eof

:: 查看日志
:view_logs
docker ps --format "table {{.Names}}" | findstr /r "^%PROJECT_NAME%$" > nul
if %errorlevel% equ 0 (
    call :print_info "查看容器日志..."
    docker logs -f %PROJECT_NAME%
) else (
    call :print_error "容器未运行"
    exit /b 1
)
goto :eof

:: 显示帮助信息
:show_help
echo 用法: %~nx0 [选项]
echo.
echo 选项:
echo   build     构建Docker镜像
echo   run       运行Docker容器
echo   compose   使用docker-compose运行
echo   stop      停止并删除容器
echo   clean     清理Docker镜像
echo   logs      查看容器日志
echo   help      显示此帮助信息
echo.
echo 示例:
echo   %~nx0 build     # 构建镜像
echo   %~nx0 run       # 运行容器
echo   %~nx0 compose   # 使用docker-compose运行
echo   %~nx0 stop      # 停止容器
echo   %~nx0 clean     # 清理镜像
echo   %~nx0 logs      # 查看日志
goto :eof

:: 主函数
:main
call :check_docker

if "%~1"=="build" (
    call :build_image
) else if "%~1"=="run" (
    call :build_image
    call :run_container
) else if "%~1"=="compose" (
    call :run_compose
) else if "%~1"=="stop" (
    call :stop_container
) else if "%~1"=="clean" (
    call :stop_container
    call :clean_image
) else if "%~1"=="logs" (
    call :view_logs
) else if "%~1"=="help" (
    call :show_help
) else if "%~1"=="--help" (
    call :show_help
) else if "%~1"=="-h" (
    call :show_help
) else (
    call :print_error "无效选项: %~1"
    call :show_help
    exit /b 1
)

endlocal 